package com.myway.controller;

import com.alibaba.fastjson.JSONObject;
import com.myway.dto.common.JsonData;
import com.myway.pojo.CharterFlight;
import com.myway.service.CharterFlightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by wsq on 2018/8/17.
 */
@Controller
public class CharterFlightController {

    @Autowired
    private CharterFlightService charterFlightService;

    @RequestMapping("/en/addCharter")
    @ResponseBody
    public JsonData addCharter(@RequestBody String charterFlightInfo) throws Exception{

        System.out.println("CharterFlightInfo:" + charterFlightInfo);
        JSONObject jsonObject = JSONObject.parseObject(charterFlightInfo);

        CharterFlight charterFlight = new CharterFlight();
        charterFlight.setFirmName(jsonObject.getString("firmName"));
        charterFlight.setEmail(jsonObject.getString("email"));
        charterFlight.setMobile(jsonObject.getString("mobile"));
        charterFlight.setFax(jsonObject.getString("fax"));
        charterFlight.setType(jsonObject.getString("type"));
        charterFlight.setDeparturePoint(jsonObject.getString("departurePoint"));
        charterFlight.setArrivalPoint(jsonObject.getString("arrivalPoint"));
        charterFlight.setNumberOfPassenger(jsonObject.getInteger("numberOfPassenger"));

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        Date departureTime = dateFormat.parse(jsonObject.getString("departureTime"));
        Date arrivalTime = dateFormat.parse(jsonObject.getString("arrivalTime"));
        charterFlight.setDepartureTime(departureTime);
        charterFlight.setArrivalTime(arrivalTime);

        charterFlight.setSpecialRemarks(jsonObject.getString("specialRemarks"));

        charterFlightService.addCharterFlight(charterFlight);

        return JsonData.success();
    }


    @RequestMapping("/en/cargoService")
    @ResponseBody
    public JsonData cargoService(@RequestBody String cargoInfo){
        System.out.println("CargoInfo:" + cargoInfo);
        JSONObject jsonObject = JSONObject.parseObject(cargoInfo);
        CharterFlight charterFlight = new CharterFlight();
        charterFlight.setFirmName(jsonObject.getString("firmName"));
        charterFlight.setEmail(jsonObject.getString("email"));
        charterFlight.setMobile(jsonObject.getString("mobile"));
        charterFlight.setSpecialRemarks(jsonObject.getString("specailRemarks"));
        charterFlightService.cargoService(charterFlight);
        return JsonData.success();
    }

}
